home *** CD-ROM | disk | FTP | other *** search
-
- {*******************************************************}
- { }
- { Delphi Runtime Library }
- { }
- { Copyright (C) 1997 Borland International }
- { }
- {*******************************************************}
-
- unit ComObj;
-
- interface
-
- uses Windows, ActiveX, SysUtils;
-
- type
-
- { Forward declarations }
-
- TComObjectFactory = class;
-
- { COM server abstract base class }
-
- TComServerObject = class(TObject)
- protected
- function CountObject(Created: Boolean): Integer; virtual; abstract;
- function CountFactory(Created: Boolean): Integer; virtual; abstract;
- function GetHelpFileName: string; virtual; abstract;
- function GetServerFileName: string; virtual; abstract;
- function GetServerKey: string; virtual; abstract;
- function GetServerName: string; virtual; abstract;
- function GetTypeLib: ITypeLib; virtual; abstract;
- public
- property HelpFileName: string;
- property ServerFileName: string;
- property ServerKey: string;
- property ServerName: string;
- property TypeLib: ITypeLib;
- end;
-
- { COM class manager }
-
- TFactoryProc = procedure(Factory: TComObjectFactory) of object;
-
- TComClassManager = class(TObject)
- public
- procedure ForEachFactory(ComServer: TComServerObject;
- FactoryProc: TFactoryProc);
- function GetFactoryFromClass(ComClass: TClass): TComObjectFactory;
- function GetFactoryFromClassID(const ClassID: TGUID): TComObjectFactory;
- end;
-
- { COM object }
-
- TComObject = class(TObject, IUnknown, ISupportErrorInfo)
- protected
- { IUnknown }
- function IUnknown.QueryInterface = ObjQueryInterface;
- function IUnknown._AddRef = ObjAddRef;
- function IUnknown._Release = ObjRelease;
- { IUnknown methods for other interfaces }
- function QueryInterface(const IID: TGUID; out Obj): Integer; stdcall;
- function _AddRef: Integer; stdcall;
- function _Release: Integer; stdcall;
- { ISupportErrorInfo }
- function InterfaceSupportsErrorInfo(const iid: TIID): HResult; stdcall;
- public
- constructor Create;
- constructor CreateAggregated(const Controller: IUnknown);
- constructor CreateFromFactory(Factory: TComObjectFactory;
- const Controller: IUnknown);
- destructor Destroy; override;
- procedure Initialize; virtual;
- function ObjAddRef: Integer; virtual; stdcall;
- function ObjQueryInterface(const IID: TGUID; out Obj): Integer; virtual; stdcall;
- function ObjRelease: Integer; virtual; stdcall;
- function SafeCallException(ExceptObject: TObject;
- ExceptAddr: Pointer): HResult; override;
- property Controller: IUnknown;
- property Factory: TComObjectFactory;
- property RefCount: Integer;
- end;
-
- { COM class }
-
- TComClass = class of TComObject;
-
- { Instancing mode for COM classes }
-
- TClassInstancing = (ciInternal, ciSingleInstance, ciMultiInstance);
-
- { COM object factory }
-
- TComObjectFactory = class(TObject, IUnknown, IClassFactory, IClassFactory2)
- protected
- function GetProgID: string; virtual;
- function GetLicenseString: WideString; virtual;
- function HasMachineLicense: Boolean; virtual;
- function ValidateUserLicense(const LicStr: WideString): Boolean; virtual;
- { IUnknown }
- function QueryInterface(const IID: TGUID; out Obj): Integer; stdcall;
- function _AddRef: Integer; stdcall;
- function _Release: Integer; stdcall;
- { IClassFactory }
- function CreateInstance(const UnkOuter: IUnknown; const IID: TGUID;
- out Obj): HResult; stdcall;
- function LockServer(fLock: BOOL): HResult; stdcall;
- { IClassFactory2 }
- function GetLicInfo(var licInfo: TLicInfo): HResult; stdcall;
- function RequestLicKey(dwResrved: Longint; out bstrKey: WideString): HResult; stdcall;
- function CreateInstanceLic(const unkOuter: IUnknown; const unkReserved: IUnknown;
- const iid: TIID; const bstrKey: WideString; out vObject): HResult; stdcall;
- public
- constructor Create(ComServer: TComServerObject; ComClass: TComClass;
- const ClassID: TGUID; const ClassName, Description: string;
- Instancing: TClassInstancing);
- destructor Destroy; override;
- function CreateComObject(const Controller: IUnknown): TComObject; virtual;
- procedure RegisterClassObject;
- procedure UpdateRegistry(Register: Boolean); virtual;
- property ClassID: TGUID;
- property ClassName: string;
- property ComClass: TClass;
- property ComServer: TComServerObject;
- property Description: string;
- property ErrorIID: TGUID;
- property LicString: WideString;
- property ProgID: string;
- property Instancing: TClassInstancing;
- property ShowErrors: Boolean;
- property SupportsLicensing: Boolean;
- end;
-
- { COM object with type information }
-
- TTypedComObject = class(TComObject, IProvideClassInfo)
- protected
- { IProvideClassInfo }
- function GetClassInfo(out TypeInfo: ITypeInfo): HResult; stdcall;
- end;
-
- TTypedComClass = class of TTypedComObject;
-
- TTypedComObjectFactory = class(TComObjectFactory)
- public
- constructor Create(ComServer: TComServerObject;
- TypedComClass: TTypedComClass; const ClassID: TGUID;
- Instancing: TClassInstancing);
- function GetInterfaceTypeInfo(TypeFlags: Integer): ITypeInfo;
- procedure UpdateRegistry(Register: Boolean); override;
- property ClassInfo: ITypeInfo;
- end;
-
- { OLE Automation object }
-
- TAutoObject = class(TTypedComObject, IDispatch)
- protected
- { IDispatch }
- function GetIDsOfNames(const IID: TGUID; Names: Pointer;
- NameCount, LocaleID: Integer; DispIDs: Pointer): HResult; virtual; stdcall;
- function GetTypeInfo(Index, LocaleID: Integer; out TypeInfo): HResult; virtual; stdcall;
- function GetTypeInfoCount(out Count: Integer): HResult; virtual; stdcall;
- function Invoke(DispID: Integer; const IID: TGUID; LocaleID: Integer;
- Flags: Word; var Params; VarResult, ExcepInfo, ArgErr: Pointer): HResult; virtual; stdcall;
- end;
-
- { OLE Automation class }
-
- TAutoClass = class of TAutoObject;
-
- { OLE Automation object factory }
-
- TAutoObjectFactory = class(TTypedComObjectFactory)
- public
- constructor Create(ComServer: TComServerObject; AutoClass: TAutoClass;
- const ClassID: TGUID; Instancing: TClassInstancing);
- function GetIntfEntry(Guid: TGUID): PInterfaceEntry; virtual;
- property DispIntfEntry: PInterfaceEntry;
- property DispTypeInfo: ITypeInfo;
- end;
-
- TAutoIntfObject = class(TInterfacedObject, IDispatch, ISupportErrorInfo)
- protected
- { IDispatch }
- function GetIDsOfNames(const IID: TGUID; Names: Pointer;
- NameCount, LocaleID: Integer; DispIDs: Pointer): HResult; stdcall;
- function GetTypeInfo(Index, LocaleID: Integer; out TypeInfo): HResult; stdcall;
- function GetTypeInfoCount(out Count: Integer): HResult; stdcall;
- function Invoke(DispID: Integer; const IID: TGUID; LocaleID: Integer;
- Flags: Word; var Params; VarResult, ExcepInfo, ArgErr: Pointer): HResult; stdcall;
- { ISupportErrorInfo }
- function InterfaceSupportsErrorInfo(const iid: TIID): HResult; stdcall;
- public
- constructor Create(const TypeLib: ITypeLib; const DispIntf: TGUID);
- function SafeCallException(ExceptObject: TObject;
- ExceptAddr: Pointer): HResult; override;
- property DispIntfEntry: PInterfaceEntry;
- property DispTypeInfo: ITypeInfo;
- property DispIID: TGUID;
- end;
-
- { OLE exception classes }
-
- EOleError = class(Exception);
-
- EOleSysError = class(EOleError)
- public
- constructor Create(const Message: string; ErrorCode: Integer;
- HelpContext: Integer);
- property ErrorCode: Integer;
- end;
-
- EOleException = class(EOleSysError)
- public
- constructor Create(const Message: string; ErrorCode: Integer;
- const Source, HelpFile: string; HelpContext: Integer);
- property HelpFile: string;
- property Source: string;
- end;
-
- { Dispatch call descriptor }
-
- PCallDesc = ^TCallDesc;
- TCallDesc = packed record
- CallType: Byte;
- ArgCount: Byte;
- NamedArgCount: Byte;
- ArgTypes: array[0..255] of Byte;
- end;
-
- PDispDesc = ^TDispDesc;
- TDispDesc = packed record
- DispID: Integer;
- ResType: Byte;
- CallDesc: TCallDesc;
- end;
-
- var
- ComClassManager: TComClassManager;
-
- function CreateComObject(const ClassID: TGUID): IUnknown;
- function CreateRemoteComObject(const MachineName: WideString; const ClassID: TGUID): IUnknown;
- function CreateOleObject(const ClassName: string): IDispatch;
- function GetActiveOleObject(const ClassName: string): IDispatch;
-
- procedure OleError(ErrorCode: HResult);
- procedure OleCheck(Result: HResult);
-
- function StringToGUID(const S: string): TGUID;
- function GUIDToString(const ClassID: TGUID): string;
-
- function ProgIDToClassID(const ProgID: string): TGUID;
- function ClassIDToProgID(const ClassID: TGUID): string;
-
- procedure CreateRegKey(const Key, ValueName, Value: string);
- procedure DeleteRegKey(const Key: string);
-
- procedure DispatchInvoke(const Dispatch: IDispatch; CallDesc: PCallDesc;
- DispIDs: PDispIDList; Params: Pointer; Result: PVariant);
- procedure DispatchInvokeError(Status: Integer; const ExcepInfo: TExcepInfo);
-
- function HandleSafeCallException(ExceptObject: TObject;
- ExceptAddr: Pointer; const ErrorIID: TGUID; const ProgID,
- HelpFileName: WideString): HResult;
-
- function StringToLPOLESTR(const Source: string): POleStr;
-
- procedure ReadPropFromBag(PropBag: IPropertyBag; ErrorLog: IErrorLog;
- const Name: string; var Value: Variant);
- procedure PutPropInBag(PropBag: IPropertyBag; const Name: string;
- const Value: Variant);
- procedure RegisterComServer(const DLLName: string);
-
- implementation
-